﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Text.RegularExpressions;

namespace Readify.Management.Automation.GoGrid
{
    public class GoGridLoadBalancerInterface
    {
        internal static GoGridLoadBalancerInterface CreateFromXmlNode(XmlNode objectElementNode)
        {
            GoGridLoadBalancerInterface output = new GoGridLoadBalancerInterface();

            XmlNode ipAttributeXmlNode = GoGridHelper.GetAttributeElementFromXmlNodeAsFirstChildXmlNode(objectElementNode, "ip");
            output.IPAddress = GoGridIPAddress.CreateFromXmlNode(ipAttributeXmlNode);

            output.Port = GoGridHelper.GetAttributeElementValueFromXmlNodeAsInt32(objectElementNode, "port");

            return output;
        }

        public GoGridIPAddress IPAddress { get; set; }
        public int Port { get; set; }

        public static GoGridLoadBalancerInterface Parse(string s)
        {
            string[] parts = s.Split(':');

            GoGridLoadBalancerInterface output = new GoGridLoadBalancerInterface();
            output.IPAddress = new GoGridIPAddress();
            output.IPAddress.IPAddress = parts[0];
            output.Port = int.Parse(parts[1]);

            return output;
        }

        public override string ToString()
        {
            return string.Format("{0}:{1}", this.IPAddress, this.Port);
        }
    }
}
